package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrdersettingDao;
import com.itheima.health.dao.SetmeaDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrdersettingService;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 检查组管理
 */
@Service
@Slf4j
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    /**
     * 基于日历进行某天的预约设置
     *
     * @param
     * @return
     */
    public List<Map> getOrderSettingByMonth(Integer year, Integer month) {
      /*  String dateBegin = date + "-1";//2023-5-1
        String dateEnd = date + "-31";//2023-5-31
        Map map = new HashMap();
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        List<OrderSetting> list = ordersettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number", orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations", orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;*/
        LocalDate begin = LocalDate.of(year, month, 1);
        LocalDate end = begin.with(TemporalAdjusters.lastDayOfMonth());
        List<OrderSetting> orderSettingList = ordersettingDao.getOrderSettingByMonth(begin, end);
        List<Map> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            //将date转换为localdate
            Date date = orderSetting.getOrderDate();
            LocalDate ld = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            //返回前端数据格式map
            Map map = new HashMap();
            map.put("date", ld.getDayOfMonth());
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     */
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count > 0){
            //当前日期已经进行了预约设置，需要进行修改操作
            ordersettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //当前日期没有进行预约设置，进行添加操作
            ordersettingDao.add(orderSetting);
        }
    }

    /**
     * 上传文件
     * @param List
     */
    @Transactional
    public void add(List<OrderSetting> List) {
        if(List != null && List.size() > 0){
            for (OrderSetting orderSetting : List) {
                //检查此数据（日期）是否存在
                long count = ordersettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){
                    //已经存在，执行更新操作
                    ordersettingDao.editNumberByOrderDate(orderSetting);
                }else{
                    //不存在，执行添加操作
                    ordersettingDao.add(orderSetting);
                }
            }
        }
    }

}